package com.kitty.game.pay.service;

import com.kitty.common.bean.PayGateWay;
import com.kitty.common.mysql.DataSource;
import com.kitty.common.mysql.DataSourceType;
import com.kitty.common.mysql.TargetDataSource;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.ServerService;
import com.kitty.game.pay.PayDataPool;
import com.kitty.game.pay.model.PayNotifyInfo;
import com.kitty.game.role.model.Account;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Dao;
import org.nutz.lang.random.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class OrderService {
    @Autowired
    Dao dao;
    @DataSource(value = DataSourceType.SLAVE)
    public boolean handleOrder(PayNotifyInfo payNotifyInfo) {
        PayGateWay payGateWay = new PayGateWay();
        Account account =SpringUtils.getAccountService().getAccount(payNotifyInfo.getAccount());
        payGateWay.setAccount(account.getUsername());
        payGateWay.setRmb(payNotifyInfo.getMoney());
        payGateWay.setMoney(payNotifyInfo.getMoney());
        payGateWay.setCreate_time(new Date());
        payGateWay.setState(0);
        //payGateWay.setOrderid(R.UU32());
        payGateWay.setChannel(account.getChannelCode());
        String name = SpringUtils.getBean(ServerService.class).getServer().getName();
        payGateWay.setServername(name);
        dao.insert(payGateWay);
//        Logger logger = LoggerFactory.getLogger(getClass());
//        /**判断订单是否已经处理*/
//        if (isOrderHandled(payNotifyInfo.getTradeNo())) {
//            log.info("订单已经处理，订单信息:{}", JsonUtils.object2String(payNotifyInfo));
//            return true;
//        }
//        /**订单是否正在处理*/
//        if (isOrderHandling(payNotifyInfo.getTradeNo())) {
//            log.info("订单正在处理，订单信息:{}", JsonUtils.object2String(payNotifyInfo));
//            return false;
//        }
//        // TODO 订单签名信息
//
//
//        /**给账号加元宝*/
//        /**如果不在线，则加载到内存处理*/
//        Account account = SpringUtils.getAccountService().getAccount(payNotifyInfo.getAccount());
//        if (account == null) {
//            log.info("订单对应账号不存在，订单信息:{}", JsonUtils.object2String(payNotifyInfo));
//
//            /**从正在处理订单中删除*/
//            PayDataPool.HANDLING_ORER_MAP.remove(payNotifyInfo.getTradeNo());
//            return false;
//        }
//
//
//        int gold = payNotifyInfo.getGold() > 0 ? payNotifyInfo.getGold() : calPayGold(payNotifyInfo.getMoney());
//        Role role = SpringUtils.getPlayerService().getPlayerBy(payNotifyInfo.getRoleUid());
//        if (role != null) {
//            SpringUtils.getRoleService().addGold(role, gold, Reason.PAY);
//
//            /** 产生一个充值成功事件 */
//            EventDispatcher.getInstance().fireEvent(new PaySuccessEvent(EventType.PAY_SUCESS, role, payNotifyInfo.getMoney(), gold));
//            // 单笔充值送
//            int score = 0;
//            if (payNotifyInfo.getMoney() == 98){
//                score = 5;
//            }else if (payNotifyInfo.getMoney() == 198){
//                score = 15;
//            }else if (payNotifyInfo.getMoney() == 588){
//                score = 50;
//            }else if (payNotifyInfo.getMoney() == 1000){
//                score = 100;
//            }else if (payNotifyInfo.getMoney() == 3000){
//                score = 400;
//            }else if (payNotifyInfo.getMoney() == 5000){
//                score = 700;
//            }else if (payNotifyInfo.getMoney() == 10000){
//                score = 1500;
//            }
////            if (score > 0){
////                SpringUtils.getRoleService().addRechargeScore(role, score);
////                MessagePusher.pushMessage(role,new RespNotifyMiscEx("单笔充值赠送#R"+score+"积分"));
////            }
//        } else {
//            /**订单角色找不到时，直接给账号加元宝*/
//            account.addGold(gold, Reason.PAY);
//            Db4CommonService.getInstance().add2Queue(account);
//        }
//
//        /**首日充值活动*/
//        //这里是充值监控嘛
//       // SpringUtils.getBean(NewChargeActivityService.class).addChongzhiRankCount(role,payNotifyInfo);
//        /**加入到已经处理的订单*/
//        PayDataPool.HANDLED_ORDER_MAP.put(payNotifyInfo.getTradeNo(), System.currentTimeMillis());
//        PayOrder payOrder = new PayOrder(payNotifyInfo.getTradeNo(), payNotifyInfo.getRoleUid()+"", payNotifyInfo.getAccount(), payNotifyInfo.getCreateTime(), payNotifyInfo.getMoney());
//        Db4CommonService.getInstance().add2Queue(payOrder);
//        logger.error("充值记录=={}", Json.toJson(payOrder, JsonFormat.compact()));
//        /**从正在处理订单中删除*/
//        PayDataPool.HANDLING_ORER_MAP.remove(payNotifyInfo.getTradeNo());

        return true;
    }

    /**
     * 订单是否已经处理
     */
    private boolean isOrderHandled(String tradeNo) {


        return false;
    }

    /**
     * 订单是否正在处理
     */
    private boolean isOrderHandling(String tradeNo) {
        if (PayDataPool.HANDLING_ORER_MAP.containsKey(tradeNo)) {
            return true;
        }

        PayDataPool.HANDLING_ORER_MAP.put(tradeNo, System.currentTimeMillis());
        return false;
    }

    /**
     * 计算充值获得的对应元宝
     */
    private int calPayGold(int money) {
//        单笔充值小于30 1：2100
//        单笔充值大于等于30 1：2200
//        单笔充值大于等于98 1：2300
//        充值大于等于198 1：2400
//        充值大于等于328 1：2600
//        充值大于等于648 1：2800
        if (money <= 0) {
            return 0;
        }
        int rate = 3200;
        if (money >= 10000) {
            money += 3000;
        } else if (money >= 5000) {
            money += 1500;
        } else if (money >= 4000) {
            money += 1200;
        } else if (money >= 3000) {
            money += 900;
        } else if (money >= 2000) {
            money += 600;
        } else if (money >= 1000) {
            money += 300;
        } else if (money >= 648) {
            money += 150;
        } else if (money >= 328) {
            money += 70;
        } else if (money >= 198) {
            money += 25;
        } else if (money >= 98) {
            money += 5;
        }
        return money * rate;
    }
}
